#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-


import os
from fm_util import Util
from fm_util import SingletonProcess
from fm_param import FmConst
from usr_session import UsrSession


class Main:

    def run(self, access, ui, business):
        if access == "physical":
            sessionId = "seat-main"
        else:
            sessionId = "0"

        os.makedirs(FmConst.getRunDir(sessionId), exist_ok=True)
        try:
            with SingletonProcess(FmConst.getSingletonLockFile(sessionId)):
                Util.forceDelete(FmConst.getTmpDir(sessionId))
                os.makedirs(FmConst.getTmpDirOnHdd(sessionId), exist_ok=True)
                UsrSession(sessionId, access, ui, business).run()
                return 0
        except SingletonProcess.AlreadyExistException:
            print("Another session is running.")
            return 1
        finally:
            Util.forceDelete(FmConst.getTmpDirOnHdd(sessionId))
            Util.forceDelete(FmConst.getTmpDir(sessionId))
